<template>
	<view class="wrap">
		<js-error mode="bottom" ref="jsError"></js-error>
		<view class="search cu-bar">
			<u-search  v-model="barCode" :focus="focus" @confirm="search" @custom="search" @search="search" placeholder="扫一扫"  :show-action="false"></u-search>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view style="padding: 0px 10px;">
			<view  style="margin-bottom: 5px;"><text class="">存货编码：</text>{{model.basInv.cinvcode}}</view>
			<view style="margin-bottom: 5px;"><text class="">存货代码：</text>{{model.basInv.cinvaddcode}}</view>
			<view style="margin-bottom: 5px;"><text class="">存货名称：</text>{{model.basInv.cinvname}}</view>
			<view style="margin-bottom: 5px;"><text class="">仓库：</text>{{model.cwhcode}}</view>
			<view style="margin-bottom: 5px;"><text class="">数量：</text>{{model.qty}}</view>
		</view>
		<scroll-view class="scroll-list bg-white" scroll-y="true" :style="[{height:stockListHeight + 'px'}]">
			<u-table>
				<u-tr>
					<u-th width="50px" ></u-th>
					<u-th>货位</u-th>
					<u-th>批次</u-th>
					<u-th>数量</u-th>
					<u-th>下架数量</u-th>
				</u-tr>
				<view style=" height: 50vh;overflow: scroll;">
					<view v-for="(item,index) in list" :key='index'>
						<u-tr v-if="list">
							<u-td width="50px" >
								<u-checkbox style="display: flex;justify-content: center;"  @change="checkboxChange(item,index)" v-model="item.checked"></u-checkbox>
							</u-td>
							<u-td style="word-break: break-all;"> {{item.areacode +'/' + item.cposname }}</u-td>
							<u-td>{{item.cbatch}}</u-td>
							<u-td>{{item.iquantity}}</u-td>
							<u-td><u-input placeholder="请输入入库数量" v-model="item.iqty" required type="number"></u-input></u-td>
						</u-tr>
					</view>
					<u-empty v-if="!list" text="暂无数据" mode="data"></u-empty>
				</view>

			</u-table>
		</scroll-view>
		<view class="footer">
			<view class="confrim-btn padding padding-bottom-sm flex bg-white">
				<u-button  type="primary" class="btn" style="width: 100%;" @click="submit">提交</u-button>
			</view>
		</view>
	</view>
</template>
	
<script>
	export default {
		data() {
			return {
				focus: true,
				stockListHeight: 0,
				barCode:'AA01|01GD01600500I010|ST12 1.6*140*C|Z1110620220905(2)220907112841001|004|949|1|202209071128|0AAA||20220905(2)||9103277|11106||||||',//AA01|01GD01600500I010|ST12 1.6*140*C|Z1110620220905(2)220907112841001|004|949|1|202209071128|0AAA||20220905(2)||9103277|11106||||||
				model:{
					basInv:{},
					cwhcode:'',
					qty:'',
				},
				list: [],
				listData:[],
			}
		},
		
		onShow() {},
		onLoad(params) {
			var _self = this;
			uni.getSystemInfo({
				success: (e) => { // resu 可以获取当前屏幕的高度
					_self.stockListHeight = e.windowHeight - uni.upx2px(450);
				},
				fail: (res) => {}
			});
		},
		methods: {
			loadData(){
				this.$u.api.qt.getXjXpp({barCode:this.barCode}).then(res => {
					this.barCode = '';
					this.model = res.xppData;
					this.list = res.list;
				})
			},
			toScan(){
				var _this = this;
				uni.scanCode({ 
					onlyFromCamera: true,
					success: function(res) {
						this.barCode = res.result;
						this.search();
					}.bind(this)
				});
			},
			search(){
				this.loadData(this.barCode);
			},
			checkboxChange(item,index){
				for (var i=0;i<this.listData.length;i++){
					if(this.listData[i].cposcode == item.cposcode){
						this.listData.splice(i,1);
					}
				}
				if(item.checked==false || item.checked==undefined){
					this.listData.push(item)
				}
			},
			
			// 提交
			async submit() {
				var list = [];
				for(var i = 0;i<this.listData.length;i++){
					var item = {
						cposcode:this.listData[i].cposcode,
						cbatch:this.listData[i].cbatch,
						iqty:this.listData[i].iqty,
						retrace:this.model.djno,
						sum:this.model.djnoW,
					}
					list.push(item);
				}
				if(list.length > 0){
					let data = {
						...this.model,
						list: list
					}
					this.$u.api.qt.saveOutToU8(data).then(res => {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success: (res) => {}
						});
						if(res.result == 'true'){
							uni.navigateBack({
								delta: 1,	
							})
						}
					})
				}else{
					this.$refs.jsError.showError('','请勾选下架货位！','error');
				}
			},
		}
	}
</script>

<style scoped>
	@import '../asd.css';
	/deep/ .u-checkbox__label{
		display: none ;
	}
</style>